package gen.tools.svn;

import gen.tools.Widgets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChkSVN4subsys extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ChkSVN4subsys() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		
		/*
		 * 0 尚未測試	
		 * 1 沒有為此子系統設定	
		 * 2 主機不能連
		 * 3 帳密不通
		 * 4 連線正常
		 * */
		int resSVN4subsys = 0;	
		
		boolean isSource = Boolean.parseBoolean( request.getParameter("isSource") );	//是找原始碼檔還是部署檔
		String subsysId = request.getParameter("subsysId");	// 子系統ID，先用它來比對
		String isMR = request.getParameter("svnType");		// 看是MR還是OL
		
		try {
			boolean isExistSVN4Subsys = wid.isExistSVN4Subsys(subsysId,isMR.equalsIgnoreCase("MR"),isSource );
			
			if( isSource ) {
				if (isExistSVN4Subsys) { //有進行設定，開始取得連線資訊，驗證如何連線
					ArrayList<String> connSVNInfo = wid.getSVN4SubsysConnInfo(subsysId,isMR.equalsIgnoreCase("MR")?3:4);
					resSVN4subsys = wid.chkSVNconnection(connSVNInfo.get(0),connSVNInfo.get(1),connSVNInfo.get(2) );
				} else {	//根本尚未對subsys做設定
					resSVN4subsys=1;
				}
			} else {
				ArrayList<String> connSVNInfo = wid.getSVN4SubsysConnInfo(subsysId,isMR.equalsIgnoreCase("MR")?6:7);
				resSVN4subsys = wid.chkSVNconnection(connSVNInfo.get(0),connSVNInfo.get(1),connSVNInfo.get(2) );
			}
		} catch (Exception e) {
			wid.errLog(e);
		}
		
		response.getWriter().write( String.valueOf(resSVN4subsys) );
	}
}
